package script

import (
	"game/core"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/solarlune/resolv"
)

type EnemyBullet struct {
	core.Sprite
	ProjectileMovement *core.ProjectileMovement
	Animation          *core.Animation
}

func NewEnemyBullet(position resolv.Vector) *EnemyBullet {

	b := &EnemyBullet{
		Sprite: core.Sprite{
			Position: &position,
			Scale:    &resolv.Vector{X: 1, Y: 1},
			Image:    EnemyBulletImg[0],
		},
	}
	position.Y += b.GetWH().Y
	b.SetPosition(position)
	wh := b.GetWH()
	b.Collision = resolv.NewRectangle(b.GetCenter().X, b.GetCenter().Y, wh.X, wh.Y)

	b.ProjectileMovement = core.NewProjectileMovement(b, core.ProjectileMovementOption{Lifetime: 5})

	b.Animation = core.NewAnimation(&core.AnimationOption{Frames: []*core.Frame{
		{Image: EnemyBulletImg[0]},
		{Image: EnemyBulletImg[1]},
		{Image: EnemyBulletImg[2]},
	}, Delay: 5, IsLooping: true})

	return b
}

func (b *EnemyBullet) Update(dt float64) error {
	b.ProjectileMovement.Update(dt)
	//b.UpdateTimer(dt)
	b.Animation.Update(dt)
	b.ProjectileMovement.MoveVec(resolv.NewVector(0, 3))

	// b.Collision.IntersectionTest(resolv.IntersectionTestSettings{
	// 	TestAgainst: b.Collision.SelectTouchingCells(1).FilterShapes(),
	// 	OnIntersect: func(set resolv.IntersectionSet) bool {
	// 		if set.OtherShape.Tags().Has(core.TagWall) {
	// 			b.Destory()
	// 		}
	// 		return true
	// 	},
	// })
	return nil
}

func (b *EnemyBullet) Draw(screen *ebiten.Image) {
	b.Animation.Draw(screen, b.Scale, b.Position)

	// op := &ebiten.DrawImageOptions{}
	// op.GeoM.Scale(b.Scale.X, b.Scale.Y)
	// op.GeoM.Translate(b.Position.X, b.Position.Y)

	// screen.DrawImage(b.Image, op)
}
